//
//  MapViewController.m
//  BosidengIphone
//
//  Created by  on 12-1-5.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MapViewController.h"
#import "Place.h"
#import "PlaceMark.h"

@implementation MapViewController
@synthesize mapView;
@synthesize places;
@synthesize location;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [places release];
    [mapView release];
    [super dealloc];
}

#pragma mark - View lifecycle


//解码google地图api3.0返回的路径数据http://maps.google.com/maps/api/directions/json?origin=121.2345,78.4521&destination=121.2345,78.4521
- (NSMutableArray *)decodePolyLine:(NSMutableString *)encoded {
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    NSInteger lat = 0;
    NSInteger lng = 0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
        NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
        printf("[%f,", [latitude doubleValue]);
        printf("%f]", [longitude doubleValue]);
        CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[latitude floatValue]] autorelease];
        [array addObject:loc];
    }
    return array;
}

- (void)centerMap {
    MKCoordinateRegion region;
    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 90;
    CLLocationDegrees minLon = 180;
    if (location == nil) {
        if ([places count] == 0) {
            return;
        }
    } else {
        maxLat = location.latitude;
        maxLon = location.longitude;
        minLat = location.latitude;
        minLon = location.longitude;
    }

    for (Place *place in places) {
        if (place.latitude > maxLat) {
            maxLat = place.latitude;
        }
        if (place.latitude < minLat) {
            minLat = place.latitude;
        }
        if (place.longitude > maxLon) {
            maxLon = place.longitude;
        }
        if (place.longitude < minLon) {
            minLon = place.longitude;
        }
    }
    region.center.latitude = (maxLat + minLat) / 2;
    region.center.longitude = (maxLon + minLon) / 2;
    region.span.latitudeDelta = ((maxLat == minLat) ? 0.01f : (maxLat - minLat));
    region.span.longitudeDelta = ((maxLon == minLon) ? 0.01f : (maxLon - minLon));
    [mapView setRegion:region animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //mapView.showsUserLocation = YES;
    
    for (Place *place in places) {
        PlaceMark *mark = [[PlaceMark alloc] initWithPlace:place];
        [mapView addAnnotation:mark];
        [mark release];
    }
    
    [self centerMap];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.mapView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
